package hjt.com.module_test.widget;

import android.content.Context;

import androidx.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

import hjt.com.module_test.R;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by hjt on 2019/4/19.
 */

public class MyHeader extends ViewGroup implements RefreshHeader {

    private View headView;
    private GifImageView imgGif;
    private GifDrawable giftDrawable;

    public MyHeader(Context context) {
        super(context);
        initView(context);
    }

    public MyHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MyHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    private void initView(Context context) {
        headView = LayoutInflater.from(context).inflate(R.layout.srl_header, null);
        imgGif = headView.findViewById(R.id.img_gif);
        imgGif.setImageResource(R.mipmap.aa);
        giftDrawable = (GifDrawable) imgGif.getDrawable();
        giftDrawable.stop();
        giftDrawable.seekTo(0);
    }

    @NonNull
    @Override
    public View getView() {
        return headView;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {

    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {

    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        giftDrawable.reset();
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        giftDrawable.stop();
        giftDrawable.seekTo(0);
        return 500;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {

    }
}
